package cn.lanqiao.book.controller;



import cn.lanqiao.book.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/*
* 用于处理 发送验证码请求（Ajax请求）
* username 用户名
* email 用户邮箱
*
* */
@WebServlet("/sendVerification")
public class SendVerificationCode extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置请求的编码
        req.setCharacterEncoding("UTF-8");
        //设置响应的编码
        resp.setContentType("text/html;charset=UTF-8");
        //生成验证密码
        int number=(int)(Math.random()*10000);
        //获取收件人
        String email=req.getParameter("email");
        //发送密码
        UserServiceImpl.getInstance().sendVerificationCode(email,number);

        //将验证密码输入到session中，验证用户输入的验证码是否正确
        HttpSession session=req.getSession();
        session.setAttribute("number",number);

        resp.getWriter().write("发送成功");

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
